<?php

class Pages extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();

		// Check if the logged user is an administrator
		$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'pages_model', 'pages');
		//$this->load->module_model('admin', 'category_model', 'category');

		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'page');
		//$this->load->library('fckeditor',array('instanceName' => 'content'));
	}

	// Public methods
	public function index()
	{

		$lang_working = load_language_working();
		$data['pages'] = $this->pages->get_pages($lang_working['id']);

		$this->_template['page']	= 'pages/list';

		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function create()
	{
		//$this->load->library('fckeditor',array('instanceName' => 'content','BasePath' => base_url().'system/plugins/fckeditor/','ToolbarSet' => 'Basic'));
		$lang_working = load_language_working();
		$this->load->helper('fckeditor');

		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[200]|xss_clean');
		//$this->form_validation->set_rules('excerpt', 'lang:form_excerpt', 'required|prep_for_form');
		//$this->form_validation->set_rules('content', 'lang:form_content', 'prep_for_form');
		//$this->form_validation->set_rules('categories', 'lang:form_categories', 'required');
		$this->form_validation->set_rules('tags', 'lang:form_tags', 'xss_clean');
		$this->form_validation->set_rules('status', 'lang:form_status', 'required|xss_clean');
		$this->form_validation->set_rules('allow_comments', 'lang:form_allow_comments', 'numeric');
		$this->form_validation->set_rules('sticky', 'lang:form_sticky', 'numeric');

		$this->form_validation->set_error_delimiters('', '<br />');
		
		$data['settings']['allow_comments'] = 'checked';
			
		if ($this->form_validation->run() == TRUE)
		{
			$this->pages->create_page($lang_working['id']);
			
			$this->session->set_flashdata('message', lang('successfully_created'));

			redirect('admin/pages', 'refresh');
		}
			
		$this->_template['page']	= 'pages/create';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function edit($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$this->load->helper('fckeditor');	
		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[200]');
		//$this->form_validation->set_rules('excerpt', 'lang:form_excerpt', 'required|prep_for_form');
		//$this->form_validation->set_rules('content', 'lang:form_content', 'prep_for_form');
		//$this->form_validation->set_rules('categories', 'lang:form_categories', 'required|xss_clean');
		$this->form_validation->set_rules('status', 'lang:form_status', 'required|xss_clean');
		$this->form_validation->set_rules('sticky', 'lang:form_sticky', 'numeric');		
		$this->form_validation->set_error_delimiters('', '<br />');	
		$data['post']						= $this->pages->get_page($id);
		$data['settings']['sticky'] = ($data['post']['sticky'] == 1) ? 'checked' : '';
		//var_dump ($data['post']);
		if ($this->form_validation->run() == TRUE)
		{
			$this->pages->edit_page($id);
			
			$this->session->set_flashdata('message', lang('successfully_edited'));

			redirect('admin/pages', 'refresh');
		}
		$this->_template['page']	= 'pages/edit';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function delete($news_id)
	{
		$this->posts->delete_news($news_id);
		$this->posts->delete_newscategory($news_id);
		
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/news', 'refresh');
	}
}

/* End of file posts.php */
/* Location: ./application/modules/admin/controllers/posts.php */